﻿using UnityEngine;
using System.Collections;
using System.Xml;
using System.Collections.Generic;

public class CombinationSuitXmlParser : XMLParserBase
{
    private void ReadSingleCombinationSuitBasicInfo(CombinationSuitInfo temp, XmlNode itemNode)
    {
        if (itemNode.Attributes["star_level"] != null)
        {
            temp.m_star_level = XmlConvert.ToInt32(itemNode.Attributes["star_level"].Value);
        }

        if (null != itemNode.Attributes["intro"])
        {
            temp.m_intro = itemNode.Attributes["intro"].Value;
        }

        if (null != itemNode.Attributes["show_view"])
        {
            temp.m_showview = itemNode.Attributes["show_view"].Value;
        }
        if (null != itemNode.Attributes["show_suit_id"])
        {
            temp.m_show_suit_id = XmlConvert.ToInt32(itemNode.Attributes["show_suit_id"].Value);
        }
        XmlAttribute iconAttr = itemNode.Attributes["icon"];
        if (null != iconAttr)
        {
            temp.m_iconfile = iconAttr.Value;
        }
        else
        {
            string strTempID = temp.m_id.ToString();
            int nZoreNum = Consts.ICON_NAME_LENGTH - strTempID.Length;
            if (0 < nZoreNum)
            {
                strTempID = strTempID.PadLeft(Consts.ICON_NAME_LENGTH, '0');
            }

            temp.m_iconfile = strTempID + "_icon";
        }

        if (null != itemNode.Attributes["limit"])
        {
            temp.m_is_limit = XmlConvert.ToBoolean(itemNode.Attributes["limit"].Value);
        }
        // 图标特效
        if (null != itemNode.Attributes["effect_src"])
        {
            temp.m_effect_src = itemNode.Attributes["effect_src"].Value;
        }
        // 服饰等级
        if (null != itemNode.Attributes["item_level"])
        {
            temp.m_item_level = XmlConvert.ToInt32(itemNode.Attributes["item_level"].Value);
        }

        if (null != itemNode.Attributes["advanced"])
        {
            temp.m_is_advanced = XmlConvert.ToBoolean(itemNode.Attributes["advanced"].Value);
        }
        if (null != itemNode.Attributes["feature"])
        {
            string featureStr = itemNode.Attributes["feature"].Value;
            temp.m_feature_list.Clear();
            if (!string.IsNullOrEmpty(featureStr))
            {
                List<int> featureList = new List<int>();
                string[] strArray = featureStr.Split(',');
                for (int i = 0; i < strArray.Length; i++)
                {
                    featureList.Add(int.Parse(strArray[i]));
                }
                temp.m_feature_list.Add(0, featureList);
            }
        }
        XmlParseHelper.ParseAttrIntDefault(itemNode, "combin_type", ref temp.m_combin_type, 0);
}

    private void ReadSingleCombinationSuitDressList(CombinationSuitInfo temp, XmlNode itemNode)
    {
        XmlNode dress_list = itemNode.SelectSingleNode("dresses");
        if (null == dress_list)
        {
            return;
        }
        XmlNodeList dressListNodeList = dress_list.ChildNodes;
        for (int j = 0; j < dressListNodeList.Count; ++j)
        {
            XmlNode dressListNode = dressListNodeList[j] as XmlNode;
            if (null == dressListNode)
            {
                continue;
            }
            List<int> dressList = new List<int>();

            XmlAttributeCollection dressListColl = dressListNode.Attributes;
            for (int i = 0; i < dressListColl.Count; ++i)
            {
                XmlAttribute dressAttr = dressListColl[i] as XmlAttribute;
                if (null == dressAttr)
                {
                    continue;
                }
                int dressID = XmlConvert.ToInt32(dressAttr.Value);
                if (0 == dressID)
                    continue;

                dressList.Add(dressID);
            }
            if (0 >= dressList.Count)
                continue;

            temp.m_suit_dress_list.Add(dressList);
        }
    }

    private void ReadSingleCombinationSuitWeaerInfo(CombinationSuitInfo temp, XmlNode itemNode)
    {
        XmlNode wearInfoNode = itemNode.SelectSingleNode("wear_info");
        if (null == wearInfoNode)
        {
            return;
        }

        XmlNodeList wearNodeList = wearInfoNode.ChildNodes;
        for (int g = 0; g < wearNodeList.Count; ++g)
        {
            XmlNode wearNode = wearNodeList[g] as XmlNode;
            if (null == wearNode)
            {
                continue;
            }

            CWearInfo wearInfo = new CWearInfo();
            wearInfo.m_nPieceNum = XmlConvert.ToInt32(wearNode.Attributes["piece_num"].Value);

            temp.m_wear_info_map[wearInfo.m_nPieceNum] = wearInfo;
        }

    }
    private void ReadSingleCombinationSuitConfig(ref CombinationSuitInfo temp, XmlNode itemNode, int nCombSuitTemplateID)
    {
        temp = new CombinationSuitInfo();
        temp.m_id = nCombSuitTemplateID;
        temp.m_sex = XmlConvert.ToInt32(itemNode.Attributes["gender"].Value);
        temp.m_name = itemNode.Attributes["name"].Value;

        ReadSingleCombinationSuitBasicInfo(temp, itemNode);

        ReadSingleCombinationSuitDressList(temp, itemNode);

        ReadSingleCombinationSuitWeaerInfo(temp, itemNode);

        ReadSingleCombinationSuitBadgeInfo(temp, itemNode);
    }
    public override object ParserXML(System.Xml.XmlNode rootNode, byte[] bytValue)
    {
        var info = new CombinationConfigData();
        XmlNodeList itemNodeList = rootNode.ChildNodes;
        for (int i = 0; i < itemNodeList.Count; ++i)
        {
            XmlNode itemNode = itemNodeList[i] as XmlNode;
            if (null != itemNode)
            {
                int nCombSuitTemplateID = 0;
                if (!XmlParseHelper.ParseAttrInt(itemNode, "id", ref nCombSuitTemplateID)) { 
                    return false;
                }
                CombinationSuitInfo a_comb = new CombinationSuitInfo();
                ReadSingleCombinationSuitConfig(ref a_comb, itemNode, nCombSuitTemplateID);

                if (info.combination_suit_info_map.ContainsKey(nCombSuitTemplateID))
                {
#if USE_LOGWRAPPER
                    LogWrapper.LogError("CombinationSuitXmlParser id repeated : id, {0}", nCombSuitTemplateID);
#endif
                    continue;
                }
                info.combination_suit_info_map[nCombSuitTemplateID] = a_comb;
            }
        }
        return info;
    }

    public void ReadSingleCombinationSuitBadgeInfo(CombinationSuitInfo temp, XmlNode itemNode)
    {
        XmlNode badgesInfoNode = itemNode.SelectSingleNode("badges");
        if (null == badgesInfoNode)
        {
            return;
        }

        XmlNodeList badgeListNodeList = badgesInfoNode.ChildNodes;

        for (int i = 0; i < badgeListNodeList.Count; ++i)
        {
            XmlNode badgeNode = badgeListNodeList[i] as XmlNode;
            if (null == badgeNode)
            {
                continue;
            }        

            int badgeId = XmlHelper.ParseAttrInt(badgeNode, "id");
            temp.m_badge_list.Add(badgeId); 
        }
    }
}

